//
//  MovieTableViewController.m
//  LoLBox
//
//  Created by lanou3g on 15/5/31.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "MovieTableViewController.h"
#import "MovieViewController.h"
@interface MovieTableViewController ()

@property (nonatomic , strong) NSMutableArray  * dataArray;
@end

@implementation MovieTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self parseUrl];
    
    [self settings];
}
-(void)settings
{
    UIBarButtonItem * leftBI = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(leftBIWay)];
    
    self.navigationItem.leftBarButtonItem = leftBI;
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MovieTableViewCell" bundle:nil] forCellReuseIdentifier:@"moviecell"];

    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shuidi.png"]];
    
    self.tableView.backgroundView = imageView;
    
}

-(void)leftBIWay
{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)parseUrl
{
    self.dataArray = [NSMutableArray array];
    
    NSString * strUrl = [NSString stringWithFormat:@"http://112.124.12.126:8080/LOLWebServers/api/getvideolist?title=&key=%@&num=15&page=0&flag=0",_str];
    
    NSURL * url = [NSURL URLWithString:strUrl];
    
    NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    
    __weak MovieTableViewController * movieTVC = self;
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
       
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
       // NSLog(@"%@",array);
        
        for (NSDictionary * dict in array) {
            
            CollegeModle * modle = [[CollegeModle alloc]init];
            
            [modle setValuesForKeysWithDictionary:dict];
            
          // NSLog(@"%@",modle);
            
            [movieTVC.dataArray addObject:modle];
            
           // NSLog(@"-----%@",_dataArray);
        }
        [movieTVC.tableView reloadData];
    }];


}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MovieTableViewCell * cell =[tableView dequeueReusableCellWithIdentifier:@"moviecell" ];
    
    cell.backgroundColor = [UIColor clearColor];
    
    CollegeModle * modle = _dataArray[indexPath.row];
    
    cell.modle = modle;
    
    self.navigationItem.title = modle.key;
    
    return cell;
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return _dataArray.count;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     CollegeModle * modle = _dataArray[indexPath.row];

//    MovieViewController * movieVc = [[MovieViewController alloc]init];
//    
//    movieVc.url = modle.url;
    
 //   [self.navigationController pushViewController:movieVc animated:YES];

    
    MovieViewController * movieVc = [[MovieViewController alloc]init];
    
    movieVc.url = modle.url;
    
    [self.navigationController pushViewController:movieVc animated:YES];

    
    
//    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:modle.url]];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Table view delegate

// In a xib-based application, navigation from a table can be handled in -tableView:didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here, for example:
    // Create the next view controller.
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:<#@"Nib name"#> bundle:nil];
    
    // Pass the selected object to the new view controller.
    
    // Push the view controller.
    [self.navigationController pushViewController:detailViewController animated:YES];
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
